package com.ypz.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ypz.reggie.common.R;
import com.ypz.reggie.dto.SetmealDto;
import com.ypz.reggie.entity.Category;
import com.ypz.reggie.entity.Setmeal;
import com.ypz.reggie.entity.SetmealDish;
import com.ypz.reggie.service.CategoryService;
import com.ypz.reggie.service.SetmealDishService;
import com.ypz.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 套餐的管理
 */
@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetmealController {
    @Autowired
    private SetmealService setmealService;

    @Autowired
    private SetmealDishService setmealDishService;

    @Autowired
    private CategoryService categoryService;

    /**
     * 新增套餐
     *
     * @param setmealDto
     * @return
     */
    @PostMapping
    @CacheEvict(value = "setmealCache",allEntries = true)
    public R<String> save(@RequestBody SetmealDto setmealDto) {

        setmealService.saveWithDish(setmealDto);

        return R.success("保存成功");
    }

    /**
     * 分页查询
     *
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, String name) {
        //查询套餐信息
        Page<Setmeal> setmealPage = new Page<>(page, pageSize);

        //查询套餐信息的查询条件
        LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();
        lqw.like(name != null, Setmeal::getName, name);
        lqw.orderByDesc(Setmeal::getUpdateTime);

        //执行查询
        setmealService.page(setmealPage, lqw);

        Page<SetmealDto> setmealDtoPage = new Page<>();

        //将套餐信息复制到setmealDtoPage
        BeanUtils.copyProperties(setmealPage, setmealDtoPage, "records");

        List<Setmeal> setmealList = setmealPage.getRecords();
        List<SetmealDto> setmealDtoList = new ArrayList<>();

        for (Setmeal setmeal : setmealList) {

            SetmealDto setmealDto = new SetmealDto();

            //1.套餐id,这一步其实可以忽略，页面没有要求，只要求了第2步
            Long id = setmeal.getId();
            //查询套餐与菜品的关联关系
            LambdaQueryWrapper<SetmealDish> setmealDishLambdaQueryWrapper = new LambdaQueryWrapper<>();
            setmealDishLambdaQueryWrapper.eq(SetmealDish::getSetmealId, id);

            List<SetmealDish> setmealDishList = setmealDishService.list(setmealDishLambdaQueryWrapper);
            setmealDto.setSetmealDishes(setmealDishList);

            //2.分类id
            Long categoryId = setmeal.getCategoryId();

            Category category = categoryService.getById(categoryId);
            String categoryName = category.getName();

            //设置setmealDto中的categoryName
            setmealDto.setCategoryName(categoryName);

            //3.将setmeal复制到setmealDto
            BeanUtils.copyProperties(setmeal, setmealDto);

            //4.将封装好的setmealDto放入setmealDtoList
            setmealDtoList.add(setmealDto);

        }

        setmealDtoPage.setRecords(setmealDtoList);

        return R.success(setmealDtoPage);

    }

    /**
     * 删除套餐
     *
     * @return
     */
    @CacheEvict(value = "setmealCache",allEntries = true)
    @DeleteMapping
    public R<String> delete(@RequestParam List<Long> ids) {

        setmealService.removeWithDish(ids);

        return R.success("删除成功");
    }

    /**
     * 根据套餐id查套餐
     * @param setmeal
     * @return
     */
    @GetMapping("/list")
    @Cacheable(value = "setmealCache", key = "#setmeal.categoryId+'_'+#setmeal.status")
    public R<List<Setmeal>> list(Setmeal setmeal){

        if (setmeal.getCategoryId() != null && setmeal.getStatus() == 1){
            LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();
            lqw.eq(Setmeal::getCategoryId, setmeal.getCategoryId());
            lqw.eq(Setmeal::getStatus, 1);

            List<Setmeal> list = setmealService.list(lqw);

            return R.success(list);

        }

        return R.error("查询失败");

    }

    /**
     * 修改套餐售卖状态
     * 停售、起售
     * @param ids
     * @param status
     * @return
     */
    @PostMapping("/status/{status}")
    public R<String> modifyStatus(Long[] ids,@PathVariable Integer status){

        //减少访问数据库次数，但是浪费了服务器空间
        List<Setmeal> setmealList = new ArrayList<>();

        for (Long id : ids) {

            Setmeal setmeal = new Setmeal();
            setmeal.setStatus(status);
            setmeal.setId(id);

            setmealList.add(setmeal);
        }

        boolean b = setmealService.updateBatchById(setmealList);

        if (b){
            return R.success("账号状态已改变");
        }

        return R.error("未知错误");
    }

    /**
     * 更具套餐id查询，套餐和套餐与菜品的关联信息
     * @param setmealId
     * @return
     */
    @GetMapping("/{setmealId}")
    public R<SetmealDto> modifySetmeal(@PathVariable Long setmealId){

        //更具id查询套餐
        Setmeal setmeal = setmealService.getById(setmealId);

        //更具套餐id查询与之相关连的菜品
        LambdaQueryWrapper<SetmealDish> lqw = new LambdaQueryWrapper<>();
        lqw.eq(SetmealDish::getSetmealId, setmealId);

        List<SetmealDish> list = setmealDishService.list(lqw);

        //给setmealDto复制
        SetmealDto setmealDto = new SetmealDto();

        BeanUtils.copyProperties(setmeal, setmealDto);

        setmealDto.setSetmealDishes(list);

        return R.success(setmealDto);

    }

    /**
     * 修改套餐与套餐关联关系
     * @return
     */
    @PutMapping
    @CacheEvict(value = "setmealCache",allEntries = true)
    public R<String> update(@RequestBody SetmealDto setmealDto){
        Boolean aBoolean = setmealService.updateWithDish(setmealDto);

        if (aBoolean){
            return  R.success("更新成功");
        }

        return R.error("未知错误");
    }

}
